import { getCategory } from "../../api";
import { baseImgUrl } from "../../setting";
const app = getApp();
Page({
  data: {
    activeTab: 0,
    tabs: [{anchor:51,image:"20220723153135564750.jpg",sub:[],name:'品牌手机'}],
    showTabs: true
  },
  onLoad(option) {
    this.setData({
      parentid:option.url
    })
    this.getList();
    
  },
  onShow() {
    this.setData({
      showTabs: false
    })
    setTimeout(() => {
      this.setData({
        showTabs: true
      })
    }, 100);
  },
  // 获取分类列表
  getList() {
    const { parentid } = this.data
    getCategory({
      parentid:parentid||''
    }).then(res => {
      if (res) {
        const list = res.map(k => {
          const items = k.sub ? k.sub.map(p => {
            return { text: p.name, id: p.id, icon: baseImgUrl + p.image }//子级
          }) : [];
          return { title: k.name, anchor: k.id, image: k.image, sub: items }//父级
        });
        this.setData({
          tabs: list
        })
      }
    })

  },
  onItemClick(ev) {
    const index1 = this.data.activeTab;
    const index2 = ev.detail.index;
    const cateId = this.data.tabs[index1].sub[index2].id;
    my.navigateTo({ url: '../../pagesA/pages/productList/productList?cateId=' + cateId });
  },
  handleTabClick(index) {
    this.setData({
      activeTab: index,
    });
  },
  handleChange(index) {
    this.setData({
      activeTab: index,
    });
  },
  onChange(index) {
    this.setData({
      activeTab: index,
    });
  },
  goToSearch() {
    my.navigateTo({ url: '../search/search' });
  }
});
